package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 四办清单
 */

public class SiBan_List extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        //创建详情页信息对象，该对象用于获取和封装详情页数据
        AllInfo allInfo = new AllInfo();
        //依次遍历 马上办、网上办、就近办、一次办 “四办”列表
        for (int pdmsType = 1 ; pdmsType <= 4 ; pdmsType++){
            //列表当前页
            int currentPage = 1;
            //列表总页数
            int pags = 0;
            //遍历列表每一页
            while (currentPage != pags){
                String listUrl = "https://wssp.hainan.gov.cn/wssp3/zwgk/checklist";
                JSONObject listHeader = new JSONObject();
                listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/govpublic_detail?qdCategory=sbqd&title=%E5%9B%9B%E5%8A%9E%E6%B8%85%E5%8D%95");
                JSONObject listParmas = new JSONObject();
                listParmas.put("regionId", "HZ2881f4424539dd0142453c856b0025");
                listParmas.put("pageNumber", currentPage);
                listParmas.put("pageSize", 10);
                listParmas.put("pdmsType", pdmsType);
                listParmas.put("qdCategory", "sbqd");
                listParmas.put("cfg_dept_id", pdmsType);
                String listStrResponse = postFormReq(listUrl,listHeader,listParmas);
                JSONObject listResponse = JSONObject.parseObject(listStrResponse);
                JSONArray deptList = listResponse.getJSONObject("data").getJSONArray("list");
                for (Object o : deptList){
                    JSONObject dept = (JSONObject) o;
                    JSONArray subdatas = dept.getJSONArray("subdatas");
                    for (Object o1 : subdatas){
                        JSONObject subdata = (JSONObject) o1;
                        //向详情页信息对象传入请求参数
                        allInfo.setId(subdata.getString("id"));
                        allInfo.setSourcekey(subdata.getString("sourcekey"));
                        //获取封装好的详情页数据
                        allInfo.init();
                        JSONObject result;
                        try {
                            result = allInfo.getAllInfo();
                        } catch (Exception e) {
                            break;
                        }
                        System.out.println(currentPage+"\n"+result);
                        return processResult(
                                null,
                                listStrResponse,
                                null,
                                null,
                                result,
                                "海南省",
                                "政务公开-四办清单"
                        );
                    }
                }
                currentPage++;
                //更新总页数
                pags = listResponse.getJSONObject("data").getJSONObject("page").getInteger("pageCount") + 1;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        SiBan_List siBan_list = new SiBan_List();
        siBan_list.getDataDemo();
    }

}
